'use strict'

const playerHandle = require('../core/playerHandle')
const route = require('../module/route')
const playerSessionMgr = require('../core/playerSessionMgr')
const attackHandle = require('../core/attackHandle')
const Monster = require('../component/monster')
const SUN = require('../base/sun')
let mapConfig = SUN.totalConfig.map
let npcConfig = SUN.totalConfig.npc
let taskConfig = SUN.totalConfig.task
let dungeonConfig = SUN.totalConfig.dungeon
let resourcesConfig = SUN.totalConfig.resources

//战斗
route.set('battle', function (player, msg, next) {
    let dungeonId = msg.dungeonId
    let ret = SUN.verification(msg, [
        ['dungeonId', 'number'],
    ])
    if (ret) {
        next(103, ret)
        return
    }
    let dungeonInfo = dungeonConfig[dungeonId]
    if (!dungeonInfo) {
        next(1)
        return
    }
    let monsters = dungeonInfo.monster
    let dungeonIndex = player.basicInfo.dungeonIndex
    if (player.basicInfo.dungeonIndex > monsters.length - 1) {
        next(2)
        return
    }
    let monster = new Monster(monsters[dungeonIndex])
    let fightMsg = attackHandle.attack(player, monster)
    if (player.basicInfo.dungeonIndex <= monsters.length - 1) {
        player.basicInfo.dungeonIndex += 1
    }
    next(0, fightMsg)
}, {
    1: '副本不存在',
    2: '无法战斗'
})

//采集
route.set('collection', function (player, msg, next) {
    let resourceId = msg.resourceId
    let ret = SUN.verification(msg, [
        ['resourceId', 'number'],
    ])
    if (ret) {
        next(103, ret)
        return
    }
    let resourceInfo = resourcesConfig[resourceId]
    if (!resourceInfo) {
        next(1)
        return
    }
    let prop = resourceInfo.prop
    ret = player.bag.addItem(prop)
    if (ret === -1) {
        next(2)
        return
    }
    next(0)
}, {
    1: '资源不存在',
    2: '背包空间不足'
})

//采矿
route.set('mining', function (player, msg, next) {
    next(0)
})

//买东西
route.set('buyProp', function (player, msg, next) {
    next(0)
})

//探险
route.set('explore', function (player, msg, next) {
    next(0)
})

//进入副本
route.set('enterCopy', function (player, msg, next) {
    next(0)
})

//解密
route.set('decrypt', function (player, msg, next) {
    next(0)
})

//祈祷
route.set('pray', function (player, msg, next) {
    next(0)
})


//获取地图信息
route.set('getMapInfo', function (player, msg, next) {
    next(0, mapConfig)
})

//进入地图
route.set('enterMap', function (player, msg, next) {
    let mapId = msg.mapId
    let mapInfo = mapConfig[mapId]
    if (mapInfo) {
        player.basicInfo.mapId = mapId
        player.basicInfo.dungeonIndex = 0
        next(0, mapInfo)
    } else {
        next(1)
    }
}, {
    1: '地图mapId不存在'
})

//获取地图信息
route.set('getMapById', function (player, msg, next) {
    let mapId = msg.mapId
    let mapInfo = mapConfig[mapId]
    if (mapInfo) {
        next(0, mapInfo)
    } else {
        next(1)
    }
}, {
    1: '地图mapId不存在'
})

//获取npc的信息
route.set('getNpcInfo', function (player, msg, next) {
    let npcId = msg.npcId
    let npcInfo = npcConfig[npcId]
    if (npcInfo) {
        next(0, npcInfo)
    } else {
        next(1)
    }
}, {
    1: 'npcId不存在'
})

//获取任务信息
route.set('getTaskInfo', function (player, msg, next) {
    let taskId = msg.taskId
    let taskInfo = taskConfig[taskId]
    if (taskInfo) {
        next(0, taskInfo)
    } else {
        next(1)
    }
}, {
    1: '任务id不存在'
})

//获取任务列表
route.set('getTaskList', function (player, msg, next) {
    next(0, player.task)
})

//领取任务
route.set('receiveTask', function (player, msg, next) {
    let taskId = msg.taskId
    let taskInfo = taskConfig[taskId]
    if (taskInfo) {
        let ret = player.task.receiveTask(taskId)
        next(ret)
    } else {
        next(2)
    }
}, {
    1: '任务id不能重复领取',
    2: '任务不存在'
})

//交任务
route.set('completeTask', function (player, msg, next) {
    let taskId = msg.taskId
    let ret = player.task.completeTask(taskId, player)
    next(ret)
}, {
    1: '任务不存在'
})